package org.yelongframework.zip.executor;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.yelongframework.commons.io.FileNameUtil;
import org.yelongframework.zip.file.WriteZipFile;

/**
 * 抽象实现
 * 
 * @since 1.0.0
 */
public abstract class AbstractZipExecutor implements ZipExecutor {

	// ==================================================压缩==================================================

	@Override
	public void zip(WriteZipFile writeZipFile, File file) throws IOException {
		try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
			zip(writeZipFile, fileOutputStream);
		}
	}

	// ==================================================解压缩==================================================

	@Override
	public void unzip(File file) throws IOException {
		unzip(file, x -> x);
	}

	@Override
	public void unzip(File file, String unzipRootPath) throws IOException {
		if (StringUtils.isBlank(unzipRootPath)) {
			unzip(file, x -> x);
		} else {
			unzip(file, x -> FileNameUtil.getFilePath(unzipRootPath, x));
		}
	}

}
